﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml;
using DSS_XNA.Engine.Error;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Drawing;
using DSS_XNA.Engine.Graphics;

namespace DSS_XNA.Engine.Actor.Actors
{
    public class ChanChan : IActor
    {
        #region IActor Members

        public override Game1 Engine
        {
            get { return _engine; }
            set { _engine = value; }
        }
        private Game1 _engine = null;

        public override Sprite Sprite
        {
            get { return _sprite; }
            set { _sprite = value; }
        }
        private Sprite _sprite = null;

        public float Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }
        private float _speed = 1.0f;

        public KeyboardState LastKeyboardState
        {
            get { return _lastKeyboardState; }
            set { _lastKeyboardState = value; }
        }
        private KeyboardState _lastKeyboardState = new KeyboardState();

        public override int Load(XmlDocument doc, Game1 engine, string texture, float width, float height)
        {
            if (doc == null)
                throw new DSSException("'doc' is null.");
            if (engine == null)
                throw new DSSException("'engine' is null.");

            this.Engine = engine;

            this.Sprite = new Sprite();
            this.Sprite.WidthInMeters = width;
            this.Sprite.HeightInMeters = height;
            this.Sprite.Texture = Texture2D.FromFile(this.Engine.GraphicsDevice, texture);

            this.WidthInMeters = width;
            this.HeightInMeters = height;

            this.Speed = (float)Convert.ToDouble(doc.SelectSingleNode("actor/speed").InnerText);

            return 0;
        }

        public override int Update(GameTime gameTime, KeyboardState ks, MouseState ms, GamePadState gs)
        {
            if (ks.IsKeyDown(Keys.Up))
                this.Position.Y -= World.World.Meter * (float)gameTime.ElapsedGameTime.TotalSeconds * this.Speed;
            if (ks.IsKeyDown(Keys.Down))
                this.Position.Y += World.World.Meter * (float)gameTime.ElapsedGameTime.TotalSeconds * this.Speed;

            if (ks.IsKeyDown(Keys.Left))
                this.Position.X -= World.World.Meter * (float)gameTime.ElapsedGameTime.TotalSeconds * this.Speed;
            if (ks.IsKeyDown(Keys.Right))
                this.Position.X += World.World.Meter * (float)gameTime.ElapsedGameTime.TotalSeconds * this.Speed;


            this.LastKeyboardState = ks;

            return 0;
        }

        public override int Render(SpriteBatch spriteBatch)
        {
            if (spriteBatch == null)
                throw new DSSException("'spriteBatch' is null.");

            Microsoft.Xna.Framework.Rectangle dest = new Microsoft.Xna.Framework.Rectangle();
            dest.Width = (int)this.Sprite.WidthInPixels;
            dest.Height = (int)this.Sprite.HeightInPixels;
            dest.X = (int)this.Position.X;
            dest.Y = (int)this.Position.Y;

            spriteBatch.Draw(this.Sprite.Texture, dest, Microsoft.Xna.Framework.Graphics.Color.White);

            return 0;
        }

        #endregion
    }
}
